

using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Linq;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;
using Abp.Domain.Services;

using Yozeev;
using Yozeev.SystemConfig;
using Abp.Domain.Entities.Auditing;
using Abp.Authorization.Users;

namespace Yozeev.SystemConfig.DomainService
{
    /// <summary>
    /// PowerRole领域层的业务管理
    ///</summary>
    public class PowerRoleManager :YozeevDomainServiceBase, IPowerRoleManager
    {

		private readonly IRepository<PowerRole, Guid> _repository;
		private readonly IRepository<Power, Guid> _power;

		private readonly IRepository<UserRole, long> _userRoleRepository;

		/// <summary>
		/// PowerRole的构造方法
		///</summary>
		public PowerRoleManager(
			IRepository<PowerRole, Guid> repository
			,IRepository<Power, Guid> power
			, IRepository<UserRole, long> userRoleRepository
		)
		{
			_repository =  repository;
			_power = power;
			_userRoleRepository = userRoleRepository;
		}


		/// <summary>
		/// 初始化
		///</summary>
		public void InitPowerRole()
		{
			throw new NotImplementedException();
		}

		// TODO:编写领域业务代码

		/// <summary>
		/// 根据用户Id查询数据查看类型
		/// </summary>
		/// <param name="UserId"></param>
		/// <returns></returns>
		public ViewTypeEnum GetViewType<T>(long UserId)
		{
			string typeName = typeof(T).Name.ToString();
			var userRole = _userRoleRepository.FirstOrDefault(m => m.UserId == UserId);
			var query = from powerRole in _repository.GetAll().Where(m => m.Power.Category == PowerTypeEnum.Data && m.RoleId == userRole.RoleId && m.Power.Menu.MenuCode == typeName)
						select new
						{
							powerRole.Power,
						};
			if(query.Count()<=0)
			{
				if (userRole.RoleId <= 2)
				{
					return ViewTypeEnum.viewallorgan;
				}
				return ViewTypeEnum.viewown;
			}
			var power = query.FirstOrDefault().Power;
			return (ViewTypeEnum)Enum.Parse(typeof(ViewTypeEnum), power.PowerCode);
		}

		/// <summary>
		/// 根据用户Id查询菜单列表
		/// </summary>
		/// <param name="UserId"></param>
		/// <returns></returns>
		public List<Menu> GetMenuList<T>(long UserId)
		{
			var userRole = _userRoleRepository.FirstOrDefault(m => m.UserId == UserId);
			var query = from powerRole in _repository.GetAll().Where(m => m.Power.Category == PowerTypeEnum.MENU && m.RoleId == userRole.RoleId && m.Power.PowerName == "ownmenu")
						select new
						{
							powerRole.Power.Menu,
						};
			if (query.Count() <= 0)
			{
				if (userRole.RoleId <= 2)
				{
					return new List<Menu>();
				}
				return new List<Menu>();
			}
			return query.Select(m=>m.Menu).ToList();
		}

		/// <summary>
		/// 根据用户权限生成菜单查询条件
		/// </summary>
		/// <param name="UserId"></param>
		/// <returns></returns>
		public Expression<Func<Menu, bool>> GetMenuExpressionByUser(long UserId)
		{
			Expression<Func<Menu, bool>> whereExp = entity => 1 == 1;
			string typeName = "ownmenu";//拥有菜单
			//查询用户角色
			var userRole = _userRoleRepository.FirstOrDefault(m => m.UserId == UserId);
			var query = (from powerRole in _repository.GetAll().Where(m => m.Power.Category == PowerTypeEnum.MENU && m.RoleId == userRole.RoleId && m.Power.PowerCode == typeName)
						 select powerRole.Power.MenuId).ToList();

			if (query.Count() <= 0)
			{
				if (userRole.RoleId <= 2)
				{
					return whereExp;
				}
				whereExp = entity => entity.MenuCode == "Dashboard";
				return whereExp;
			}
			whereExp = entity => query.Contains(entity.Id);
			return whereExp;
		}

		/// <summary>
		/// 根据用户和菜单id查找权限操作代码
		/// </summary>
		/// <param name="UserId"></param>
		/// <param name="menuId"></param>
		/// <returns></returns>
		public List<string> GetPowerCodeByUser(long UserId, Guid menuId)
		{
			var userRole = _userRoleRepository.FirstOrDefault(m => m.UserId == UserId);
			var query = (from powerRole in _repository.GetAll().Where(m => m.Power.Category == PowerTypeEnum.OPERATE && m.RoleId == userRole.RoleId && m.Power.MenuId == menuId)
						 select powerRole.Power.PowerCode).ToList();

			if (query.Count() <= 0)
			{
				return new List<string>();
			}
			return query;
		}

		#region 根据角色ID和权限代码查询是否有对应的权限功能
		/// <summary>
		/// 根据角色ID和权限代码查询是否有对应的权限功能
		/// </summary>
		/// <param name="userId">用户id</param>
		/// <returns></returns>
		public PowerRole GetIsPowerInfo(int roleId = 0, string powerCode = "")
		{
			var powers = _power.GetAll()
				.Where(p => p.PowerCode == powerCode)
				.FirstOrDefault();

			var powerrole = _repository.GetAll()
				.Where(p => p.RoleId == roleId && p.PowerId == powers.Id)
				.FirstOrDefault();

			return powerrole;
		}
		#endregion


	}
}
